package org.hiucung.lib_base.base.adapter

import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewParent
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

/**
 * @title: CommonViewHolder
 * @projectName OrangeAiVoiceApp
 * @description: TODO
 * @author Kicc
 * @date 2021/11/18 13:53
 */
class CommonViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    private var mView: SparseArray<View?> = SparseArray()

    companion object {
        //获取holder对象
        fun getViewHolder(parent: ViewGroup, layoutId: Int): CommonViewHolder {
            val itemView: View =
                LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
            return CommonViewHolder(itemView)
        }
    }

    fun getView(viewId: Int) : View{
        var view = mView[viewId]
        if (view == null){
            view = itemView.findViewById(viewId)
            mView.put(viewId,view)
        }
        return view!!
    }

    fun setText(viewId: Int, text: String){
        (getView(viewId) as TextView).text = text
    }

    fun setImageResource(viewId: Int, resId:Int){
        (getView(viewId) as ImageView).setImageResource(resId)
    }
}